//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: "",
    imgUrl: baseJs.imgUrl,
    // 操作id
    actionId: "",
    // 
    pageIndex: 1,
    state:"",
    listData: [],
    maskFlag: true,
    typeName: "",
    // 
    goodsId: "",
    //出库
    outGoodsFlag: true,
    outGoodsValue: "",
    // 入库 
    inGoodsFlag: true,
    inGoodsValue: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      typeName: options.typeName
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      pageIndex:1,
      listData:[],
    })
    this.AgentGetGoodsStoreList();
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.data.pageIndex++;
    this.AgentGetGoodsStoreList();
  },

  // 
  AgentGetGoodsStoreList: function() {
    var that = this;
    var uid = this.data.uid;
    var typeName = this.data.typeName;
    var pageIndex = this.data.pageIndex;
    var listData = this.data.listData;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=AgentGetGoodsStoreList&UID=" + uid + "&PageSize=10&PageIndex=" + pageIndex + "&GoodType=" + typeName),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        console.log(JSON.parse(baseJs.decryUrl(res.data)));
        for (var i = 0; i < json.length; i++) {
          json[i].actionFlag = true;
          if ((json[i].Price.split(".")[0]).length >= 5) {
            json[i].Price = parseFloat(json[i].Price / 10000).toFixed(2) + "万";
          } else {
            json[i].Price = json[i].Price
          }
          listData.push(json[i])
        }
        that.setData({
          listData: listData,
        })
        if (json.length == 10) {
          that.setData({
            state: 'loadMore',
          })
        } else if (json.length == 0 && pageIndex == 1) {
          that.setData({
            state: 'noData',
          })
        } else if (json.length < 10) {
          that.setData({
            state: 'noMore',
          })
        } else if (json.length == 0 && pageIndex > 1) {
          that.setData({
            state: 'noMore',
          })
        }
      }
    })
  },
  // actionTap
  actionTap: function(e) {
    var that = this;
    var listData = this.data.listData;
    var actionId = this.data.actionId;
    for (var i = 0; i < listData.length; i++) {
      listData[e.currentTarget.dataset.index].actionFlag = false;
    }
    this.setData({
      maskFlag: false,
      listData: listData,
      actionId: e.currentTarget.dataset.index,
    })
  },
  // maskTap
  maskTap: function() {
    var that = this;
    var actionId = this.data.actionId;
    var listData = this.data.listData;
    listData[actionId].actionFlag = true;
    // for (var i = 0; i < listData.length; i++) {
    //   listData[i].actionFlag = true;
    // }
    this.setData({
      maskFlag: true,
      listData: listData,
    })
  },
  // 出库
  outGoods: function(e) {
    var that = this;
    var actionId = this.data.actionId;
    var listData = this.data.listData;
    listData[actionId].actionFlag = true;
    this.setData({
      maskFlag: true,
      outGoodsFlag: false,
      listData: listData,
      goodsId: e.currentTarget.dataset.goodsid,
    })
  },
  outGoodsCancel: function() {
    this.setData({
      outGoodsFlag: true,
    })
  },
  outGoodsValue: function(e) {
    this.setData({
      outGoodsValue: e.detail.value
    })
  },
  outGoodsConfirm: function() {
    var that = this;
    var uid = this.data.uid;
    var outGoodsValue = this.data.outGoodsValue;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=AgentGoodsStoreComeOrOut&GoodsId=" + goodsId + "&FlowType=出库" + "&GoodsNum=0&GoodsPrice=" + outGoodsValue),
      success: function(res) {
        console.log(JSON.parse(baseJs.decryUrl(res.data)));
        var json = JSON.parse(baseJs.decryUrl(res.data));
        if (json.ResultFlag == 1) {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          })
          that.setData({
            pageIndex: 1,
            listData: [],
          })
          that.AgentGetGoodsStoreList();
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 入库
  inGoods: function(e) {
    var that = this;
    var actionId = this.data.actionId;
    var listData = this.data.listData;
    listData[actionId].actionFlag = true;
    this.setData({
      inGoodsFlag: false,
      maskFlag: true,
      listData: listData,
      goodsId: e.currentTarget.dataset.goodsid,
    })
  },
  inGoodsCancel: function() {
    this.setData({
      inGoodsFlag: true,
    })
  },
  inGoodsValue: function(e) {
    this.setData({
      inGoodsValue: e.detail.value
    })
  },
  inGoodsConfirm: function() {
    var that = this;
    var uid = this.data.uid;
    var inGoodsValue = this.data.inGoodsValue;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=AgentGoodsStoreComeOrOut&GoodsId=" + goodsId + "&FlowType=入库" + "&GoodsNum=0&GoodsPrice=" + inGoodsValue),
      success: function(res) {
        console.log(JSON.parse(baseJs.decryUrl(res.data)));
        var json = JSON.parse(baseJs.decryUrl(res.data));
        if (json.ResultFlag == 1) {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          })
          that.setData({
            pageIndex: 1,
            listData: [],
          })
          that.AgentGetGoodsStoreList();
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 删除
  delGoods: function(e) {
    var that = this;
    var actionId = this.data.actionId;
    var listData = this.data.listData;
    listData[actionId].actionFlag = true;
    this.setData({
      maskFlag: true,
      listData: listData,
    })
    wx.showModal({
      title: '提示',
      content: '是否删除该产品？',
      cancelText: "取消",
      confirmText: "确定",
      success: function(res) {
        if (res.cancel) {

        } else {
          wx.request({
            url: baseJs.api + baseJs.encryUrl("method=AgentDeleteGoodsStore&GoodsId=" + e.currentTarget.dataset.goodsid),
            success: function(res) {
              console.log(JSON.parse(baseJs.decryUrl(res.data)));
              var json = JSON.parse(baseJs.decryUrl(res.data));
              if (json.ResultFlag == 1) {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
                that.setData({
                  pageIndex: 1,
                  listData: [],
                })
                that.AgentGetGoodsStoreList();
              } else {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          })
        }
      },
    })
  },
  // 修改
  updateGoods: function(e) {
    var that = this;
    var actionId = this.data.actionId;
    var listData = this.data.listData;
    listData[actionId].actionFlag = true;
    this.setData({
      maskFlag: true,
      listData: listData,
    })
    wx.navigateTo({
      url: '/packageA/yunYGoodsUpload/yunYGoodsUpload?goodsId=' + e.currentTarget.dataset.goodsid + "&uploadType=update" + "storeType=" + this.data.typeName,
    })
  },
  // 图片预览
  imgPreview: function(e) {
    wx.previewImage({
      // 当前显示图片的http链接
      current: e.currentTarget.dataset.src,
      // 需要预览的图片http链接列表
      urls: [e.currentTarget.dataset.src]
    })
  },
  goRecord:function(e){
    wx.navigateTo({
      url: '/packageA/yunYRecord/yunYRecord?goodsId=' + e.currentTarget.dataset.goodsid
    })
  },
})